package com.xinxing.city.recovery;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.entitys.BucketType;
import com.xinxing.city.recovery.entitys.Cabinet;
import com.xinxing.city.recovery.entitys.HeatBucket;
import com.xinxing.city.recovery.entitys.SetHeart;
import com.xinxing.city.recovery.entitys.UpdataApk;
import com.xinxing.city.recovery.interfaces.ISerialSendReceiveData;
import com.xinxing.city.recovery.net.BaseTask;
import com.xinxing.city.recovery.net.RetroFactory;
import com.xinxing.city.recovery.utils.CheckUpdate;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;
import com.xinxing.city.recovery.utils.VersionUtil;

import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetBaseView {
    private Handler   handler = new Handler();
    private Runnable runnable;
    Context context;
    private Gson gson;
//    private static ISerialSendReceiveData iSerialSendReceiveData;
    public static List<BucketType> buckets = new ArrayList<>();

    public SetBaseView(Context con) {
        context = con;
        gson = new Gson();
//        iSerialSendReceiveData = DI.getInstance(ISerialSendReceiveData.class);
    }
    public void setList(List<BucketType> bucketTypes){
        Log.e("photo","bucke " +bucketTypes.size());
        this.buckets = bucketTypes;
    }
    //心跳
    public void reportTime() {
        Integer time = 60;//10分鈡
        String str = (String) SharedPreferencesUtils.getParam(context, "set_heart", "");
        if (!str.equals("")) {
            Type dev = new TypeToken<SetHeart>() {
            }.getType();
            Gson gson = new Gson();
            ;
            SetHeart heart = gson.fromJson(str, dev);
            if (!heart.getHeartInterval().equals(""))
                time = Integer.valueOf(heart.getHeartInterval());
        }
        if (handler != null)
            handler.removeCallbacksAndMessages(null);
        Integer finalTime = time;

        runnable = new Runnable() {
            @Override
            public void run() {
                String cabinetStr = (String) SharedPreferencesUtils.getParam(context, "cabinet", "");
                if (!cabinetStr.equals("")) {
                    Type cType = new TypeToken<Cabinet>() {
                    }.getType();
                    Cabinet cabinet = gson.fromJson(cabinetStr, cType);
                    Map<String, Object> par = new HashMap<>();
                    par.put("cabinetCode", cabinet.getCode());
                    par.put("name", cabinet.getName());
                    if(cabinet.getStatus()==null||cabinet.getStatus().equals("")){
                        par.put("status", "00");
                    }else{
                        par.put("status", cabinet.getStatus());
                    }

                    if(cabinet.getTradingStatus()==null||cabinet.getTradingStatus().equals(""))
                        par.put("tradStatus", "00");
                    else{
                        par.put("tradStatus", cabinet.getTradingStatus());
                    }
                    List<HeatBucket> heatBuckets = new ArrayList<>();


                    for (int i = 0; i < buckets.size(); i++) {
                        BucketType b = buckets.get(i);
                        HeatBucket heatBucket = new HeatBucket();
                        heatBucket.setCode(b.getCode());
                        heatBucket.setStatus("00");

                        if (b.getCompressStatus().equals("01")) {
                            heatBucket.setCompressStatus("t");
                        } else {
                            heatBucket.setCompressStatus("f");
                        }
                        heatBucket.setHardwareCode(b.getHardwareCode());
                        DecimalFormat decimalFormat = new DecimalFormat("#0.00"); // 创建 DecimalFormat 对象并设置格式模式 "#0.00"
                        String formattedNumber = decimalFormat.format((float) (b.getWeight()));
                        heatBucket.setUsedCapacity(formattedNumber);

//                            heatBucket.setOverFlowStatus("f");
                        heatBucket.setOverFlowStatus(b.getOverFlowStatus());
                        heatBuckets.add(heatBucket);
                    }

                    Log.e("SSS", gson.toJson(heatBuckets));
                    par.put("infos", heatBuckets);

                    new BaseTask(context, RetroFactory.getJsonService().sendHeartbeat(RetroFactory.getJSON(par))).handleResponse(new BaseTask.ResponseListener() {


                        @Override
                        public void onSuccess(Object o) {

                        }

                        @Override
                        public void onFail() {

                        }
                    });

//                    iSerialSendReceiveData.openDoor(1);
                    handler.postDelayed(this, finalTime * 1000);
                }
            }
        };
        handler.postDelayed(runnable, time * 10);
    }
}
